<?php
// +----------------------------------------------------------------------
// | Author: weifashi <605403358@qq.com>
// +----------------------------------------------------------------------
namespace soft;
use think\Controller;
use think\Request;
use base\BaseMethod;
use auth\Action;
use think\Validate;

class Controls  extends Controller  {
	
	//基础定义
	private $xml;
	private $nodeId;
		
	/**
    * 构造函数自动执行
    * @return null
    */
    public function __construct($xml=null,$nodeId=null){
    	$this ->xml =  $xml;
    	$this ->nodeId = $nodeId;
    	parent::__construct();
    }
	
	/**
     * 获取示例
     * @param array $options 实例配置
     * @return static
     */
    public static function instance($xml=null,$nodeId=null)
    {
        return new self($xml,$nodeId);
    }
	
    /**
    * grid获取选项配置
    * @access public
    * @param string $id   			xml-id
    * @param string $xml 			xml-路径
    * @param array  $isEach 	直接输出，还是返回数据
    * @return array
    */
	public function getGridConfig($isEach=true){
		try {
			$data = controls\GridService::instance($this->xml,$this->nodeId)->getOption();
			//按钮权限控制
			$function = isset($data['function'])  ? $data['function'] : $xmlFile['function'].'';
			$data['btns'] =  Action::instance()->checkBtnQuth($function,$data['btns']);
			foreach($data['cols'] as $key => $cols){
				foreach($cols as $k => $col){
					if($col['buttons'][0]){
						$data['cols'][$key][$k]['buttons'] = Action::instance()->checkBtnQuth($function,$col['buttons']);
					}
				}
			}
			//返回
			return $isEach ? BaseMethod::ReSucess($data) : $data;
		} catch (\Exception $e) {
			return BaseMethod::ReError($e->getMessage());	
		}
	} 
	
	
	/**
    * grid获取数据
    * @access public
    * @param string 			$pageSize   		每页数据条数
    * @param string 			$pageIndex 		指定第几页
    * @return array
    */
	public function getGridData($isEach=true){
		try {
			$data = controls\GridService::instance()->getData();
			return $isEach ? BaseMethod::ReSucess($data) : $data;
		} catch (\Exception $e) {
			return BaseMethod::ReError($e->getMessage());	
		}
	}
	
	/**
    * grid删除数据
    * /admin/Api/Grid/deleteData
    * @access public
    * @param array $data   删除数据 [{id:1},{id:2}]
    * @return array
    */
	public function deleteGridData($data){
		try {
			$data = controls\GridService::instance()->deleteData( json_decode($data,true));
			return BaseMethod::ReSucess($data,"删除成功");
		} catch (\Exception $e) {
			return BaseMethod::ReError($e->getMessage());	
		}
	}
	
	/**
    * grid调整排序
    * @access public
    * @param string $id   			xml-id
    * @param string $xml 			xml-路径
    * @return array
    */
	public function adjustSort($moveId,$coverId,$sortField){
		try {
			$data = controls\GridService::instance()->adjustSort($moveId,$coverId,$sortField);
			return BaseMethod::ReSucess($data,"删除成功");
		} catch (\Exception $e) {
			return BaseMethod::ReError($e->getMessage());	
		}
	}
	
	/**
    * grid导出
    * @access public
    * @return array
    */
	public function exportExcel(){
		try {
			if (! function_exists('base_path')) {
			    function base_path($path = ''){
			        return env('root_path').($path ? ltrim($path, DIRECTORY_SEPARATOR) : 
			        ltrim($path, DIRECTORY_SEPARATOR));
			    }
			}
			$xls = controls\GridService::instance()->getXlsData();
			require base_path().'/extend/base/files/PHPExeclCore.php';
			\PHPExeclCore::RecursionCreateExecl($xls['xlsName'],$xls['xlsHead'],$xls['xlsData']);
		} catch (\Exception $e) {
			return BaseMethod::ReError($e->getMessage());	
		}
	}
	
	/**
    * grid更新字段
    * @access public
    * @param string $id   			xml-id
    * @param string $xml 			xml-路径
    * @return array
    */
	public function updateField($ids,$name,$value){
		try {
			controls\GridService::instance()->updateField($ids,$name,$value);
			return BaseMethod::ReSucess();
		} catch (\Exception $e) {
			return BaseMethod::ReError($e->getMessage());	
		}
	}
	
	/**
    * grid更新行
    * @access public
    * @param string $id   			xml-id
    * @param string $xml 			xml-路径
    * @return array
    */
	public function updateRow($ids,$data){
		try {
			controls\GridService::instance()->updateRow($ids,$data);
			return BaseMethod::ReSucess();
		} catch (\Exception $e) {
			return BaseMethod::ReError($e->getMessage());	
		}
	}
	
	
	/**
    * Search获取选项配置
    * @access public
    * @param string $id   			xml-id
    * @param string $xml 			xml-路径
    * @param array  $isEach 	直接输出，还是返回数据
    * @return array
    */
	public function getSearchConfig($isEach=true){
		try {
			$data = controls\SearchService::instance($this->xml,$this->nodeId)->getOption();
			return $isEach ? BaseMethod::ReSucess($data) : $data;
		} catch (\Exception $e) {
			return BaseMethod::ReError($e->getMessage());	
		}
	}
	
	
	/**
    * Form获取选项配置
    * @access public
    * @param string $id   			xml-id
    * @param string $xml 			xml-路径
    * @param array  $isEach 	直接输出，还是返回数据
    * @return array
    */
	public function getFormConfig($isEach=true){
		try {
			$data = controls\FormService::instance($this->xml,$this->nodeId)->getOption();
		    $function = isset($data['function'])  ? $data['function'] : $xmlFile['function'].'';
		    $buttons = $data['buttons']['button'];
            $data['buttons']['button'] =  Action::instance()->checkBtnQuth($function,$buttons,'button');
			return $isEach ? BaseMethod::ReSucess($data) : $data;
		} catch (\Exception $e) {
			return BaseMethod::ReError($e->getMessage()  ?: '');	
		}
	}
	
	
	/**
    * Form保存数据
    * @access public
    * @return array
    */
	public function saveFormData(){
		try {
			//令牌验证
//			$validate = new Validate;
//			$validate->rule(['__token__'=>'token']);
//	        if(!$validate->check (['__token__'=>request()->header()['token'] ]) ) {
//	        	return BaseMethod::ReError($validate->getError());	
//	        }
			//保存
			$param = request()->param();
			$data = json_decode( $param["data"],true);
			$data = controls\FormService::instance($this->xml,$data['id'])->saveData();
			//返回
			$msg = $param['mode'] != 1 ? "更新成功！" :  "新增成功！";
			$newToken = $this->request->token();
			return BaseMethod::instance($newToken)->ReSucess($data['key'],$msg);
			
		} catch (\Exception $e) {
			return BaseMethod::ReError($e->getMessage());	
		}
	}
}




